from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from flask_cors import CORS


db = SQLAlchemy()


def create_app(config_class="app.config.Config"):

    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)

    # 启用跨域
    CORS(app)

    # 注册蓝图
    from .routes import user_routes, disease_routes, workout_routes, food_routes, recipe_routes, sport_routes, appointment_routes
    app.register_blueprint(user_routes.bp, url_prefix="/api/user")
    app.register_blueprint(food_routes.bp, url_prefix="/api/food")
    app.register_blueprint(recipe_routes.bp, url_prefix="/api/recipe")
    app.register_blueprint(disease_routes.bp, url_prefix="/api/disease")
    app.register_blueprint(workout_routes.bp, url_prefix="/api/workout")
    app.register_blueprint(sport_routes.bp, url_prefix="/api/sport")
    app.register_blueprint(appointment_routes.appointment_bp, url_prefix="/api/appointment")

    with app.app_context():
        db.create_all()  # 初始化数据库
    return app